local DIY = require "packages.diy_utility.diy_utility"
local LB_U = require "packages.lb_kill_utility.utility"

local skel = fk.CreateSkill {
  name = "lb_zero__longshuang",
  tags = { Skill.Switch },
  dynamic_desc = function(self, player, lang)
    local choices = {
      "<b>深雪</b>：2",
      "<b>飞雪</b>：4",
      "<b>残雪</b>：6",
    }
    local mark = {}
    for i, v in ipairs(choices) do
      local choice = v
      if i == DIY.getSwitchState(player, "lb_zero__longshuang") then
        choice = "<font color='#E0DB2F'>" .. v .. "</font>"
      end
      table.insert(mark, choice)
    end
    if #mark == 0 then
      return "lb_zero__longshuang"
    end
    return "lb_zero__longshuang_inner:" .. table.concat(mark, "；")
  end,
}

Fk:loadTranslationTable {
  ["lb_zero__longshuang"] = "泷霜",
  [":lb_zero__longshuang"] = "<b>转换技</b>，结束阶段，你可将手牌数调整至深雪：2；飞雪：4；残雪：6；调整前你可使用任意张不能被响应的非伤害牌。你因此弃置牌后，若本回合弃牌堆的牌包含四种花色，你视为使用手牌数张冰【杀】。",

  [":lb_zero__longshuang_inner"] = "<b>转换技</b>，结束阶段，你可将手牌数调整至{1}；调整前你可使用任意张不能被响应的非伤害牌。" ..
      "你因此弃置牌后，若本回合弃牌堆的牌包含四种花色，你视为使用手牌数张冰【杀】。",

  ["#lb_zero__longshuang"] = "泷霜（%arg / %arg2）：请视为使用冰【杀】",
  --["#lb_zero__longshuang_draw"] = "泷霜：你可以摸%arg张牌",
  --["#lb_zero__longshuang_dis"] = "泷霜：你可以弃置%arg张手牌，若本回合弃牌堆的牌包含四种花色，你视为使用手牌数张冰【杀】",
  
  --11.19
  ["#lb_zero__longshuang-ask"] = "泷霜：你可以将手牌调整至 %arg",
  ["#lb_zero__longshuang-use"] = "泷霜：你即将调整手牌数至 %arg，你可以先使用任意张不能被响应的非伤害牌",

  ["$lb_zero__longshuang1"] = "胜为上，战次之。",
  ["$lb_zero__longshuang2"] = "刀已归鞘。",
  ["$lb_zero__longshuang3"] = "我将践行此刀。",
  ["$lb_zero__longshuang4"] = "恶，即斩。",
  ["$lb_zero__longshuang5"] = "势不可挡。",
  ["$lb_zero__longshuang6"] = "以此刀为誓。",
}
skel:addEffect(fk.EventPhaseStart, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player and data.phase == Player.Finish
        and (DIY.getSwitchState(player, skel.name) * 2) ~= player:getHandcardNum()
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, { 
      skill_name = skel.name, 
      prompt = "#lb_zero__longshuang-ask:::" .. DIY.getSwitchState(player, skel.name) * 2
    })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local num = DIY.getSwitchState(player, skel.name) * 2
    DIY.changeSwitchState(player, skel.name)
    player:broadcastSkillInvoke(skel.name, math.random(1, 3))
    repeat
      local Reason = Fk.currentResponseReason
      Fk.currentResponseReason = skel.name .. "_use"
      local use = room:askToPlayCard(player, {
        skill_name = skel.name,
        skip = true,
        prompt = "#lb_zero__longshuang-use:::" .. num,
      })
      Fk.currentResponseReason = Reason
      if use then
        use.disresponsiveList = room.players
        room:useCard(use)
      end
    until not use or player.dead
    if player.dead then return end
    local list = LB_U.BalanceCards(player, skel.name, num)
    if list.dis and #list.dis > 0 then
      room:notifySkillInvoked(player, skel.name, "negative")
    else
      room:notifySkillInvoked(player, skel.name, "drawcard")
      return
    end
    if player.dead or player:isKongcheng() then return end
    local suits = {}
    for _, id in ipairs(DIY.getCenterCards(room)) do
      local suit = Fk:getCardById(id).suit
      if suit ~= Card.NoSuit then
        table.insertIfNeed(suits, suit)
      end
    end
    if #suits == 4 then
      local hand = player:getHandcardNum()
      for i = 1, hand, 1 do
        if player.dead then break end
        local use = room:askToUseVirtualCard(player, {
          name = "ice__slash",
          skill_name = skel.name,
          prompt = "#lb_zero__longshuang:::" .. i .. ":" .. hand,
          cancelable = false,
          skip = true,
        })
        if use then
          room:notifySkillInvoked(player, skel.name, "offensive")
          player:broadcastSkillInvoke(skel.name, math.random(4, 6))
          room:useCard(use)
        end
      end
    end
  end,
})
--[[
skel:addEffect(fk.EventPhaseStart, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player and data.phase == Player.Finish
        and (DIY.getSwitchState(player, "lb_zero__longshuang") * 2) ~= player:getHandcardNum()
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local num = (DIY.getSwitchState(player, "lb_zero__longshuang") * 2) - player:getHandcardNum()
    if num > 0 then
      event:setCostData(self, { num = num })
      return room:askToSkillInvoke(player, {
        skill_name = skel.name,
        prompt = "#lb_zero__longshuang_draw:::" .. num
      })
    else
      num = math.abs(num)
      local cards = room:askToDiscard(player, {
        min_num = num,
        max_num = num,
        skill_name = skel.name,
        cancelable = true,
        include_equip = false,
        skip = true,
        prompt = "#lb_zero__longshuang_dis:::" .. num
      })
      if #cards > 0 then
        event:setCostData(self, { cards = cards })
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    DIY.changeSwitchState(player, skel.name)
    local room = player.room
    local cards = event:getCostData(self).cards
    local num = event:getCostData(self).num
    player:broadcastSkillInvoke(skel.name, math.random(1, 3))
    if cards and #cards > 0 then
      room:notifySkillInvoked(player, skel.name, "negative")
      room:throwCard(cards, skel.name, player)
    else
      room:notifySkillInvoked(player, skel.name, "drawcard")
      player:drawCards(num, skel.name)
      return
    end
    if player.dead or player:isKongcheng() then return end
    local suits = {}
    for _, id in ipairs(DIY.getCenterCards(room)) do
      local suit = Fk:getCardById(id).suit
      if suit ~= Card.NoSuit then
        table.insertIfNeed(suits, suit)
      end
    end
    if #suits == 4 then
      local hand = player:getHandcardNum()
      for i = 1, hand, 1 do
        if player.dead then break end
        local use = room:askToUseVirtualCard(player, {
          name = "ice__slash",
          skill_name = skel.name,
          prompt = "#lb_zero__longshuang:::" .. i .. ":" .. hand,
          cancelable = false,
          skip = true,
        })
        if use then
          room:notifySkillInvoked(player, skel.name, "offensive")
          player:broadcastSkillInvoke(skel.name, math.random(4, 6))
          room:useCard(use)
        end
      end
    end
  end,
})
--]]

skel:addEffect("prohibit", {
  prohibit_use = function (self, player, card)
    if Fk.currentResponseReason == (skel.name .. "_use") and card.is_damage_card then -- 加个后缀，和冰杀区分
      return true
    end
  end,
})

skel:addAcquireEffect(function(self, player, is_start)
  player.room:addSkill("#CenterArea")
  DIY.setSwitchState(player, skel.name, 1, 3)
end)

skel:addLoseEffect(function(self, player, is_death)
  DIY.removeSwitchSkill(player, skel.name)
end)


return skel
